package com.onlinevideo.servlet;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.onlinevideo.util.AuthUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

public class WXServlet extends BaseServlet{

        protected void wxLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            StringBuffer requestURL = req.getRequestURL();
            System.out.println("请求的地址为"+requestURL);
            //第一步：引导用户进入授权页面同意授权，获取code
            StringBuffer buffer = new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?appid=");
            buffer.append(AuthUtil.APPID);
            buffer.append("&redirect_uri=");
            buffer.append(URLEncoder.encode(AuthUtil.backUrl));//开发文档要求转换
            buffer.append("&response_type=code");
            buffer.append("&scope=snsapi_userinfo");
            buffer.append("&state=STATE#wechat_redirect");
            //授权页面地址
            //将StringBuilder转换成String
            String url=buffer.toString();
            //重定向到授权页面
            System.out.println(url);


            resp.sendRedirect(url);
        }
        protected void wxCallback(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                String code = req.getParameter("code");
            System.out.println(code);
                //获取code后，请求以下链接获取access_token
                StringBuffer buffer = new StringBuffer("https://api.weixin.qq.com/sns/oauth2/access_token?appid=");
                buffer.append(AuthUtil.APPID);
                buffer.append("&secret=");
                buffer.append(AuthUtil.APPSECRET);
                buffer.append("&code=");
                buffer.append(code);
                buffer.append("&grant_type=authorization_code");
                //通过网络请求方法来请求上面这个接口
                //将StringBuilder转换成String
                String url=buffer.toString();
            System.out.println(url);
                JSONObject jsonObject = AuthUtil.doGetJson(url);

                System.out.println("==========================jsonObject" + jsonObject);
                //从返回的JSON数据中取出access_token和openid，拉取用户信息时用
                String token = jsonObject.getStr("access_token");
                String openid = jsonObject.getStr("openid");
                // 第三步：刷新access_token（如果需要）
                // 第四步：拉取用户信息(需scope为 snsapi_userinfo)
                StringBuffer buffer1 = new StringBuffer("https://api.weixin.qq.com/sns/userinfo?access_token=");
                buffer1.append(token);
                buffer1.append("&openid=");
                buffer1.append(openid);
                buffer1.append("&lang=zh_CN");
                //通过网络请求方法来请求上面这个接口
                //将StringBuilder转换成String
                String infoUrl=buffer1.toString();
                JSONObject userInfo = AuthUtil.doGetJson(infoUrl);

                System.out.println("userInfo=======" + userInfo);
                String s = "{errcode:0,message:'成功'}";

                resp.getWriter().write(JSONUtil.toJsonStr(s));
        }
}
